package co.fitstart.weapp.api.service.impl;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.demo2do.core.persistence.GenericDaoSupport;
import com.demo2do.core.persistence.annotation.Dao;
import com.demo2do.core.utils.DateFormatUtils;
import com.demo2do.core.utils.DateUtils;

import co.fitstart.entity.coach.CoachSigninStatistic;
import co.fitstart.entity.course.schedule.SigninRecord;
import co.fitstart.entity.principal.Principal;
import co.fitstart.weapp.api.dao.SigninDao;
import co.fitstart.weapp.api.service.CourseHourRecordService;

/**
 * 
 * @author haiyan_xiao
 *
 */
@Service("courseHourRecordService")
public class CourseHourRecordServiceImpl implements CourseHourRecordService {

    @Dao
    private SigninDao signinDao;

    @Autowired
    private GenericDaoSupport genericDaoSupport;

    /*
     * (non-Javadoc)
     * @see co.fitstart.weapp.api.service.CourseHourRecordService#getTotalCourseHour(co.fitstart.entity.principal.Principal)
     */
    public BigDecimal getTotalCourseHour(Principal coach) {
        Map<String, Object> map = signinDao.count(coach);
        if(map != null && map.get("hour") != null) {
            return ((BigDecimal)map.get("hour")).subtract((BigDecimal)map.get("experienceHour")).subtract((BigDecimal)map.get("giveHour"));
        } else {
            return new BigDecimal(0);
        }
    }

    /*
     * (non-Javadoc)
     * @see co.fitstart.weapp.api.service.CourseHourRecordService#getTodayCourseHour(co.fitstart.entity.principal.Principal)
     */
    public BigDecimal getTodayCourseHour(Principal coach) {
        return signinDao.count(coach, DateUtils.parseDate(DateFormatUtils.format(new Date(), "yyyy-MM-dd"), "yyyy-MM-dd"));
    }

    /*
     * (non-Javadoc)
     * @see co.fitstart.weapp.api.service.CourseHourRecordService#listCoachSigninStatistic(co.fitstart.entity.principal.Principal)
     */
    public List<CoachSigninStatistic> listCoachSigninStatistic(Principal coach) {
        return signinDao.listCoachSigninStatistic(coach);
    }

    /*
     * (non-Javadoc)
     * @see co.fitstart.weapp.api.service.CourseHourRecordService#loadCoachSigninStatistic(java.lang.Long)
     */
    public CoachSigninStatistic loadCoachSigninStatistic(Long statisticId) {
        return genericDaoSupport.load(CoachSigninStatistic.class, statisticId);
    }

    /*
     * (non-Javadoc)
     * @see co.fitstart.weapp.api.service.CourseHourRecordService#listCoachSigninRecord(co.fitstart.entity.principal.Principal, java.lang.String)
     */
    public List<SigninRecord> listCoachSigninRecord(Principal coach, String month) {
        Date begin = DateUtils.parseDate(month + "-01", "yyyy-MM-dd");
        return signinDao.listCoachSigninRecord(coach, begin, DateUtils.lastMonthDay(begin));
    }

}
